package com.scommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scommunity.domain.Feedback;
import com.scommunity.mapper.FeedbackMapper;
import com.scommunity.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper,Feedback> implements IFeedbackService{
    @Autowired
    FeedbackMapper feedbackMapper;
    //测试存储
    /*@Cacheable(value = "list")
    public List<Feedback> findAll() {
        List<Feedback> list = feedbackMapper.selectList(null);
        return list;
    }
    @CacheEvict(value = "list" ,allEntries = true)
    public void deleteById(int i) {
        System.out.println("删除");
    }*/

   // @Cacheable(value = "page" ,key="#pagenum+'::'+#pageSize")
    @Override
    public PageInfo<Feedback> findByConditon(Feedback feedback, Integer pagenum, Integer pageSize) {
        //Page<Feedback> page = new Page<>(pagenum, pageSize);
        PageHelper.startPage(pagenum, pageSize);
        List<Feedback> list = feedbackMapper.findByCondition( feedback);
        //page.setRecords(list);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
        //两种动态查询的方式
        /*QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        if (feedback.getFbType() != null && !feedback.getFbType().isEmpty()) {
            queryWrapper.eq("fb_type", feedback.getFbType());
        }
        if (feedback.getFbStatus() != null && !feedback.getFbStatus().isEmpty()) {
            queryWrapper.eq("fb_status", feedback.getFbStatus());
        }
        if (feedback.getOwnerId() != null) {
            queryWrapper.eq("owner_id", feedback.getOwnerId());
        }
        if (feedback.getFbTime() != null ) {
            LocalDateTime startDateTime = LocalDateTime.of(LocalDate.from(feedback.getFbTime()), LocalTime.MIN);
            LocalDateTime endDateTime = LocalDateTime.of(LocalDate.from(feedback.getFbTime()), LocalTime.MAX);
            queryWrapper.between("fb_time", startDateTime, endDateTime);
        }
        return feedbackMapper.selectPage(page,queryWrapper);*/
    }
    //@Cacheable(value = "page" ,key="#pagenum+'::'+#pageSize")
    @Override
    public PageInfo<Feedback> getFeedbackByPage(Integer pagenum, Integer pageSize) {
        PageHelper.startPage(pagenum, pageSize);
        List<Feedback> list = feedbackMapper.selectAll();
        PageInfo<Feedback> pageInfo = new PageInfo(list);
        return pageInfo;
    }
    //@Cacheable(value="myfeedback",key = "#ownerId+'::'+#pageNum+'::'+#pageSize")
    @Override
    public PageInfo<Feedback> getFeedbacksByPage(Integer pageNum, Integer pageSize, Long ownerId) {
        PageHelper.startPage(pageNum, pageSize);
        /*Feedback feedback = new Feedback();
        feedback.setOwnerId(ownerId);
        List<Feedback> list = feedbackMapper.findByCondition(pages, feedback);*/
        List<Feedback> list = feedbackMapper.selectList(new QueryWrapper<Feedback>().eq("owner_id", ownerId).orderBy(true, false, "fb_time"));
        PageInfo<Feedback> pageInfo = new PageInfo(list);
        return pageInfo;
    }



}
